# Multi-stage build for CKY Agent Platform

# Frontend build stage
FROM node:18-alpine AS frontend-build
WORKDIR /app
COPY frontend/package*.json ./
RUN npm ci --only=production
COPY frontend/ ./
RUN npm run build

# Backend build stage
FROM mcr.microsoft.com/dotnet/sdk:9.0 AS backend-build
WORKDIR /src
COPY backend/CKY.AgentPlatform.sln ./
COPY backend/src/CKY.AgentPlatform.Domain/CKY.AgentPlatform.Domain.csproj ./src/CKY.AgentPlatform.Domain/
COPY backend/src/CKY.AgentPlatform.Application/CKY.AgentPlatform.Application.csproj ./src/CKY.AgentPlatform.Application/
COPY backend/src/CKY.AgentPlatform.EntityFrameworkCore/CKY.AgentPlatform.EntityFrameworkCore.csproj ./src/CKY.AgentPlatform.EntityFrameworkCore/
COPY backend/src/CKY.AgentPlatform.HttpApi/CKY.AgentPlatform.HttpApi.csproj ./src/CKY.AgentPlatform.HttpApi/
COPY backend/src/CKY.AgentPlatform.HttpApi.Host/CKY.AgentPlatform.HttpApi.Host.csproj ./src/CKY.AgentPlatform.HttpApi.Host/
RUN dotnet restore CKY.AgentPlatform.sln
COPY backend/ ./
WORKDIR /src/src/CKY.AgentPlatform.HttpApi.Host
RUN dotnet build -c Release -o /app/build

# Publish stage
FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS publish
WORKDIR /app
COPY --from=backend-build /app/build .
COPY --from=frontend-build /app/build /app/wwwroot

# Runtime stage
FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS runtime
WORKDIR /app
COPY --from=publish /app .
RUN apt-get update && apt-get install -y \
    ca-certificates \
    && rm -rf /var/lib/apt/lists/*

# Create directories
RUN mkdir -p /app/data /app/logs

EXPOSE 80 443

ENV ASPNETCORE_URLS=http://+:80;https://+:443
ENV ASPNETCORE_ENVIRONMENT=Production

ENTRYPOINT ["dotnet", "CKY.AgentPlatform.HttpApi.Host.dll"]